package com.cdc.network.view;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.cdc.network.R;
import com.cdc.network.RequestType;
import com.cdc.network.presenter.DrawBasePresenterImpl;
import com.cdc.network.tools.Constants;
import com.cdc.network.tools.Tools;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RequestFragment extends Fragment implements DrawBaseView{
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM_TITLE = "title";
    private static final String ARG_PARAM_TYPE = "type";
    @BindView(R.id.btnRequest)
    Button btnRequest;
    @BindView(R.id.tvResult)
    TextView tvResult;

    // TODO: Rename and change types of parameters
    private String title;
    private RequestType requestType;

    public RequestFragment() {
        // Required empty public constructor
    }

    private DrawBasePresenterImpl drawBasePresenter;

    public static RequestFragment newInstance(String title, RequestType requestType) {
        RequestFragment fragment = new RequestFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM_TITLE, title);
        args.putSerializable(ARG_PARAM_TYPE,requestType);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            title = getArguments().getString(ARG_PARAM_TITLE);
            requestType = (RequestType) getArguments().getSerializable(ARG_PARAM_TYPE);
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        drawBasePresenter=new DrawBasePresenterImpl();
        drawBasePresenter.attachView(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        drawBasePresenter.detachView();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.request_fragment,container,false);
        ButterKnife.bind(this,view);
        switch(requestType){
            case GET:
                btnRequest.setText(RequestType.GET.name());
                break;
            case POST:
                btnRequest.setText(RequestType.POST.name());
                break;
            case POST_JSON:
                btnRequest.setText(RequestType.POST_JSON.name());
                break;
            case UPLOAD:
                btnRequest.setText(RequestType.UPLOAD.name());
                break;
            case UPLOADS:
                btnRequest.setText(RequestType.UPLOADS.name());
                break;
        }
        return view;
    }

    public void onButtonPressed(Uri uri) {

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

    }

    @Override
    public void onDetach() {
        super.onDetach();
    }
    @OnClick(R.id.btnRequest)
    public void request(View view){
        switch(requestType){
            case GET:
                drawBasePresenter.get(Constants.PERSON(1));
                break;
            case POST:
                drawBasePresenter.post(Constants.PERSONS());
                break;
            case POST_JSON:
                drawBasePresenter.post(Constants.UPPERSON(), JSON.toJSONString(Tools.initDefaultPerson()));
                break;
            case UPLOAD:
                Map<String,Object> maps=new HashMap();
                maps.put("name","cdc");
                maps.put("id",1);
                File file=new File(Environment.getExternalStorageDirectory().getAbsoluteFile()+"/Download/1.jpg");
                Log.e("cdc",file.exists()+"");
                maps.put("file",file);
                drawBasePresenter.upload(Constants.UPLOAD(),maps);

                break;
            case UPLOADS:

                break;
        }
    }


    @Override
    public void showToast(String msg) {

    }

    @Override
    public void setResult(String result) {
        tvResult.setText(result);
    }
}
